//
//  WEXSearchView.swift
//  microBlog
//
//  Created by apple on 16/4/16.
//  Copyright © 2016年 Hill. All rights reserved.
//

import UIKit

class WEXSearchView: UIView {

    @IBOutlet weak var tf_search: UITextField!
    
    @IBOutlet weak var bt_cancel: UIButton!
    
    @IBOutlet weak var cos_trailling: NSLayoutConstraint!
    
    override func awakeFromNib() {
       
        //设置左边view的位置
        leftViw.frame.size = CGSizeMake(self.frame.height, self.frame.height)
        
        //设置左边view
        tf_search.leftView = leftViw
        
        //设置左边view的模式
        tf_search.leftViewMode = .Always
       
    }
    
    // MARK: - 懒加载放大镜
    private lazy var leftViw: UIImageView = {
        
        let searchGlass = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        searchGlass.contentMode = .Center
        
        return searchGlass
    }()
    
    // MARK: - 实现加载xib界面的类方法
    class func searchView() -> WEXSearchView {
        
        return NSBundle.mainBundle().loadNibNamed("WEXSearchView", owner: nil, options: nil).last! as! WEXSearchView
    }
    
    // MARK: - 开始编辑状态
    @IBAction func editingBegin(sender: UITextField) {
        
        //改变约束
        self.cos_trailling.constant = 50
        
        //执行动画
        UIView.animateWithDuration(2.0) { () -> Void in
            
            //执行刷新
            self.layoutIfNeeded()
        }
        
        
    }

    // MARK: - 取消编辑状态
    @IBAction func cancelEditor(sender: UIButton) {
    
        //恢复约束
        self.cos_trailling.constant = 0
        
        //执行动画
        UIView.animateWithDuration(2.0) { () -> Void in
            
            self.layoutIfNeeded()
        }
        
        self.tf_search.resignFirstResponder()
    }
    
}
